# =============================================================================
# The launcher: a small program that starts the game (crash_mom_launcher)
# =============================================================================
#
# A project of its own, NOT part of the game's build: it holds none of the
# game's code (no generated/ files, no ReXGlue runtime), so it can be built
# once and handed out ready-made, while the game itself is always built on
# the player's own PC from their own disc.
#
# What it links:
#   SDL3   window, input, the 2D renderer that draws the UI, file/URL opening
#   ImGui  the UI itself (the same library as the game's F5/F6 menus)
# Both come from the SOURCE folders inside the ReXGlue SDK submodule
# (thirdparty/rexglue-sdk/thirdparty/...): no SDK build or install needed,
# just `git submodule update --init --recursive`. SDL3 is compiled in as a
# static library, so the result is one self-contained executable.
#
# Build (from the repo root; ~1 min the first time, SDL3 is the bulk):
#   cmake -S launcher -B out/build/launcher -G Ninja -DCMAKE_BUILD_TYPE=Release \
#         -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++
#   cmake --build out/build/launcher
#   out/build/launcher/crash_mom_launcher
#
# How it finds the game: see folders.h.
# =============================================================================

cmake_minimum_required(VERSION 3.25)
project(crash_mom_launcher LANGUAGES C CXX)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Release)
endif()

# Windows: the C/C++ runtime built INTO the exe (static), so the launcher
# runs on a PC without Microsoft's runtime DLLs (testers may not have Visual
# Studio). Set before any target: SDL3 and ImGui get it too.
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")

set(SDK_THIRDPARTY ${CMAKE_CURRENT_SOURCE_DIR}/../thirdparty/rexglue-sdk/thirdparty)
if(NOT EXISTS ${SDK_THIRDPARTY}/sdl3/CMakeLists.txt)
    message(FATAL_ERROR
        "SDL3's source isn't there (${SDK_THIRDPARTY}/sdl3).\n"
        "Fetch the submodules first: git submodule update --init --recursive")
endif()

# --- SDL3, static, only what a small windowed tool needs ----------------------
set(SDL_SHARED OFF CACHE BOOL "" FORCE)
set(SDL_STATIC ON CACHE BOOL "" FORCE)
set(SDL_TEST_LIBRARY OFF CACHE BOOL "" FORCE)
set(SDL_EXAMPLES OFF CACHE BOOL "" FORCE)
set(SDL_TESTS OFF CACHE BOOL "" FORCE)
set(SDL_INSTALL OFF CACHE BOOL "" FORCE)
# Not needed: no sound, no camera, no 3D API of our own (the SDL renderer
# picks Vulkan or OpenGL by itself).
set(SDL_AUDIO OFF CACHE BOOL "" FORCE)
set(SDL_CAMERA OFF CACHE BOOL "" FORCE)
add_subdirectory(${SDK_THIRDPARTY}/sdl3 ${CMAKE_CURRENT_BINARY_DIR}/sdl3 EXCLUDE_FROM_ALL)

# --- ImGui + its SDL3 backends -------------------------------------------------
set(IMGUI_DIR ${SDK_THIRDPARTY}/imgui)
add_library(launcher_imgui STATIC
    ${IMGUI_DIR}/imgui.cpp
    ${IMGUI_DIR}/imgui_draw.cpp
    ${IMGUI_DIR}/imgui_tables.cpp
    ${IMGUI_DIR}/imgui_widgets.cpp
    ${IMGUI_DIR}/backends/imgui_impl_sdl3.cpp          # SDL3 events -> ImGui
    ${IMGUI_DIR}/backends/imgui_impl_sdlrenderer3.cpp  # ImGui -> SDL's 2D renderer
)
target_include_directories(launcher_imgui PUBLIC ${IMGUI_DIR} ${IMGUI_DIR}/backends)
target_link_libraries(launcher_imgui PUBLIC SDL3::SDL3-static)

# --- the launcher ------------------------------------------------------------
add_executable(crash_mom_launcher WIN32   # WIN32: a window, no console (Windows)
    main.cpp          # the window and the Play page
    ui_look.cpp       # colours, fonts, style (shared with the installer)
    platform_posix.cpp  # Linux: processes, places, facts (platform.h)
    platform_win.cpp    # Windows: the same
    sha256.cpp        # SHA-256 of a download (updates), no outside tool needed
    folders.cpp       # where the game, its disc files and the player's files are
    saves.cpp         # the player's saves: names, progress, last played
    game_process.cpp  # starting the game, watching it, telling a crash from a quit
    report.cpp        # "Report a problem": the chosen sessions' logs + a summary in one .zip
    settings.cpp      # the game's settings: the list (asked from the game) + user/settings.toml
    settings_page.cpp # the Settings tab
    desktop_entry.cpp # the launcher in the desktop's applications menu
    task_runner.cpp   # runs a setup step's commands one after another, with progress
    git_parts.cpp     # the SDK's libraries (submodules) without `git submodule` (Git's shell)
    setup.cpp         # the Setup tab: build tools listed, disc, SDK, game builds
    update.cpp        # new versions: the feed, download, swap (release layout)
    texts.cpp         # the launcher's texts in the player's language (languages/*.po, like the game)
)
# toml++ (header only, in the SDK submodule): reads and writes settings.toml.
# SYSTEM: its headers' own warnings (old literal-operator spelling) aren't ours.
target_include_directories(crash_mom_launcher SYSTEM PRIVATE ${SDK_THIRDPARTY}/tomlplusplus/include)
target_link_libraries(crash_mom_launcher PRIVATE launcher_imgui)
if(WIN32)
    # ole32 / shell32 / uuid: Start menu shortcuts (desktop_entry.cpp).
    target_link_libraries(crash_mom_launcher PRIVATE ole32 shell32 uuid)
endif()

# --- the Windows installer: "Mind over Recomp.exe" -----------------------------
# Installs, updates and uninstalls the game (installer.cpp). It reuses the
# launcher's building blocks (setup, update, the task runner) but none of its
# pages. Windows is its target; it also compiles on Linux (install.cpp has
# stubs there) only so its window can be looked at while working on it.
add_executable(mind_over_recomp WIN32
    installer.cpp     # the window: install / manage / uninstall, + headless switches
    install.cpp       # Windows: the Apps list, shortcuts, removing an install
    ui_look.cpp
    platform_posix.cpp
    platform_win.cpp
    sha256.cpp
    folders.cpp
    task_runner.cpp
    git_parts.cpp
    setup.cpp
    update.cpp
    texts.cpp
)
target_include_directories(mind_over_recomp SYSTEM PRIVATE ${SDK_THIRDPARTY}/tomlplusplus/include)
# Brazilian Portuguese built in (texts.h, AddBuiltIn): the installer runs
# before any of the port's files are on the PC. The .po becomes a byte array
# at configure time (re-done when the .po changes).
set(PT_BR_PO ${CMAKE_CURRENT_SOURCE_DIR}/../languages/pt_BR.po)
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${PT_BR_PO})
file(READ ${PT_BR_PO} PT_BR_HEX HEX)
string(REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1," PT_BR_BYTES "${PT_BR_HEX}")
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/builtin/pt_BR_po.inc
     "// Generated from languages/pt_BR.po by launcher/CMakeLists.txt\nstatic const unsigned char kPtBrPo[] = {${PT_BR_BYTES}0};\n")
target_include_directories(mind_over_recomp PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/builtin)
# The installer's version (shown in its header line): the first line of
# VERSION.txt, re-read when it changes.
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/../VERSION.txt)
file(STRINGS ${CMAKE_CURRENT_SOURCE_DIR}/../VERSION.txt MOR_VERSION LIMIT_COUNT 1)
string(STRIP "${MOR_VERSION}" MOR_VERSION)
target_compile_definitions(mind_over_recomp PRIVATE MOR_VERSION="${MOR_VERSION}")
target_link_libraries(mind_over_recomp PRIVATE launcher_imgui)
if(WIN32)
    # ole32 / shell32 / uuid: shortcuts (COM's IShellLink), known folders;
    # advapi32: the registry (the Apps list, PATH).
    target_link_libraries(mind_over_recomp PRIVATE ole32 shell32 uuid advapi32)
    # The exe's icon: Icons8's wrench, CC BY-ND 3.0, unchanged (icon/README.md).
    enable_language(RC)
    target_sources(mind_over_recomp PRIVATE installer.rc)
endif()
